package com.easy;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lxk on 2020/2/27.
 */
public class Solution290 {

    public boolean wordPattern(String pattern, String str) {
        String[] arr = str.split(" ");
        if (pattern.length() != arr.length) {
            return false;
        }
        Map<Character, String> chStrMap = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            if (chStrMap.keySet().contains(pattern.charAt(i))) {
                if (!chStrMap.get(pattern.charAt(i)).equals(arr[i])) {
                    return false;
                }
            } else {
                chStrMap.put(pattern.charAt(i), arr[i]);
            }
        }
        Map<String, Character> strChMap = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            if (strChMap.keySet().contains(arr[i])) {
                if (!strChMap.get(arr[i]).equals(pattern.charAt(i))) {
                    return false;
                }
            } else {
                strChMap.put(arr[i], pattern.charAt(i));
            }
        }
        return true;
    }

}
